
$(".level").click(function(){
    var ln = $(this).index();
    if(ln == 0) {
        $('.xian').hide();
        $('.town').hide();
        $('.village').hide();
    } else if(ln == 1){
        $('.xian').show();
        $('.town').hide();
        $('.village').hide();
    } else if(ln == 2) {
        $('.xian').show();
        $('.town').show();
        $('.village').hide();
    } else if(ln == 3) {
        $('.xian').show();
        $('.town').show();
        $('.village').show();
    }
});

var id = $('input[name="id"]').val();
if(id) {  //编辑操作
    var checklevel = $('input[name="level"]:checked').val();
    var cityId = $('.city').find("option:selected").val();
    var xianId = $('.xianId').val();
    var townId = $('.townId').val();
    switch(checklevel) {
        case '2':
            $('.xian').show();
            getList2(cityId,'xian');
             getList2(xianId,'town');
            break;
        case '3':            
            $('.xian').show();
            $('.town').show();
            getList2(cityId,'xian');
            getList2(xianId,'town');
            getVillage2(townId);
            break;
        case '4':
            $('.xian').show();
            $('.town').show();
            $('.village').show();
            getList2(xianId,'town');
            getList2(cityId,'xian');
            getVillage2(townId);
            break;
    }
}

function getList(e,type) {
    $('.'+type).html('');
    var name = '';
    if(type == 'xian') {
        name = '所有区县';
    } else if(type=='town') {
        name = '所有乡镇';
    } 
    if(e.value != 0) {
        $.ajax({
            url : areaurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>"+name+"</option>";
            for (let i in data){
                $item += "<option  value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.'+type).append($item);           
        });
    } else {
        $('.'+type).append("<option value='0'>"+name+"</option>");
    }
}

function getVillage(e){
    $('.village').html('');
    if(e.value != 0) {
        $.ajax({
            url : villageurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>所有村</option>";
            for (let i in data){
                $item += "<option value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.village').append($item);           
        });
    } else {
        $('.village').append("<option value='0'>所有村</option>");
    }
}

function getList2(id,type) {
    $('.'+type).html('');
    var name = '';
    var selectId = 0;
    if(type == 'xian') {
        name = '所有区县';
        selectId = $('.xianId').val();
    } else if(type=='town') {
        name = '所有乡镇';
        selectId = $('.townId').val();
    }
    if(id != 0) {
        $.ajax({
            url : areaurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':id}),
        }).done(function(data) {
            var $item = "<option value='0'>"+name+"</option>";
            for (let i in data){
                $item += "<option  value='"+i+"'";
                if(selectId == i){
                    $item += " selected ";
                }
                $item += " '>"+data[i]+"</option>";
            }
            $('.'+type).append($item);           
        });
    } else {
        $('.'+type).append("<option value='0'>"+name+"</option>");
    }
}

function getVillage2(id){
    $('.village').html('');
    var selectId = '0';
    selectId = $('.villageId').val();
    if(id != 0) {
        $.ajax({
            url : villageurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':id}),
        }).done(function(data) {
            var $item = "<option value='0'>所有村</option>";
            for (let i in data){
                $item += "<option value='"+i+"'";
                if(selectId == i) {
                    $item += ' selected';
                }
                $item += " '>"+data[i]+"</option>";
            }
            $('.village').append($item);           
        });
    } else {
        $('.village').append("<option value='0'>所有村</option>");
    }
}


$('#form-submit').on('click', function(){  //提交数据
    var form = this.form;
    if(form.title.value.trim()=='') {
        layer.msg('请输入标题');
        form.title.focus();
        return;
    }
    if(form.typeId.value==='0') {
        layer.msg("请选择分类!");
        return;
    }
    if(form.iconUrl.value=='') {
        layer.msg("请上传图片!");
        // return;
    }
    if(form.detail.value.trim()=='') {
        layer.msg("请输入详情内容题!");
        form.detail.focus();
        return;
    }
    var lenth = mdTool.sumBytes(form.detail.value);
    // 500KB
    if (lenth > 1024 * 500) {
        layer.msg('您输入的详情已超过最大范围,请修改后保存');
        // mdTool.showMessage('您输入的详情已超过最大范围,请修改后保存');
        return;
    }
    var level = form.level.value;
    switch(level) {
        case '1':
            if(form.cityId.value == 0) {
                layer.msg('请选择市');
                return;
            } else {
                form.xianId.value = 0;
                form.townId.value = 0;
                form.villageId.value = 0;
            }
            break;
        case '2':
            if(form.xianId.value == 0) {
                layer.msg('请选择区县');
                return;
            } else {
                form.townId.value = 0;
                form.villageId.value = 0;
            }
            break;
        case '3':
            if(form.townId.value == 0) {
                layer.msg('请选择乡镇');
                return;
            } else {
                form.villageId.value = 0;
            }
            break;
        case '4':
            if(form.villageId.value == 0) {
                layer.msg('请选择村子');
                return;
            }
            break;
    }
    var _data = $(form).serializeObject();
    $("#form-submit").attr("disabled","disabled");

    $.ajax({
        url : posturl,
        type : 'post',
        dataType : 'json',
        contentType : "application/json; charset=utf-8",
        data : JSON.stringify(_data),
    }).done(function(data) {
        if (data.code == 1) {
            layer.msg('保存成功');
            window.location.href= data.url; //加载页面数据
        } else if (data.code === 0 ) {  // 错误
            $("#form-submit").removeAttr("disabled");
            layer.msg(data.msg);
        }
    });
});